<template>
  <div class="app-container common-list-page">
    <el-form
      :model="resetForm"
      :rules="resetFormRules"
      ref="resetForm"
      status-icon
      label-width="100px"
    >
      <el-form-item label="旧密码：" prop="password">
        <el-input type="password" v-model="resetForm.password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码：" prop="newpwd">
        <el-input type="password" v-model="resetForm.newpwd" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="newpassword1">
        <el-input type="password" v-model="resetForm.newpassword1" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="changePwd">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import changePwd from '@/api/houseowner/changePwd'
export default {
  data() {
      var validateRepass = (rule,value,callback) => {
          if(!value){
              callback(new Error('请输入旧密码'))
          }else if(value !== localStorage.getItem('owner_password')){
              callback(new Error('您的密码不正确!'))
          }
          else{
              callback();
          }
      }
    var validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入新密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.resetForm.newpwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      resetForm: {
      //传给后台所需要的参数
        newpassword1: '',
        password: '',
        hphone:'',
      },
      resetFormRules: {
         password: [
            { required: true, validator:validateRepass, trigger: 'blur' }
        ],  
        newpwd: [
            { required: true, validator: validatePass, trigger: 'blur' }
        ],
        newpassword1: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  created(){

  },
  methods: {
    changePwd() {
      this.$refs.resetForm.validate(valid => {
        if (valid) {
            const data={
                hphone:localStorage.getItem('owner_id'),
                pwd:this.resetForm.newpassword1
            }
            changePwd.changePwd(data.hphone,data.pwd)
            .then(response => {
                var msg = response.status;
                if (msg === 200){
                    this.$notify({
                        title: '成功',
                        message: '更新成功,重新登陆',
                        position: 'top-left',
                        duration: 1500,
                        type: 'success'
                    });
                    this.$router.push('/loginHouseOwner');
                } else {
                    this.$notify.error({
                        title: '失败',
                        position: 'top-left',
                        duration: 1500,
                        message: '失败了'
                    });
                }
            })

        }
      });
    },
  },
};
</script>

<style  scoped>
.el-form {
  width: 60%;
  margin: 50px auto 0;
  text-align: center;

}
</style>
